package com.iplant.aps.service.po.sch;

import com.iplant.aps.service.po.aps.APSCapacityUsed;
import com.iplant.base.utils.general.CalendarTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.po.cfg.CFGCalendar;
import com.iplant.basic.service.po.fmc.FMCShift;
import com.iplant.basic.service.po.fmc.FMCUnavailable;
import com.iplant.pdm.service.basicenum.fpc.FPCRouteType;
import com.iplant.pdm.service.po.fpc.FPCCapacity;

import java.util.*;

public class APSMarginEntityCapacity {


    public int EntityID = 0;


    public FPCRouteType RouteType = FPCRouteType.RoutePartStation;

    public Map<String, APSMarginDayCapacity> DayCapacityMap = new HashMap<>();


    public void ClearResult() {
        for (String wKey : DayCapacityMap.keySet()) {
            DayCapacityMap.get(wKey).ClearResult();
        }
    }

    public Map<String, List<APSCapacityUsed>> DayUseCapacityMap(int wProductID) {
        Map<String, List<APSCapacityUsed>> wResult = new HashMap<>();
        for (String wKey : DayCapacityMap.keySet()) {
            wResult.put(wKey, DayCapacityMap.get(wKey).getProductUsedTime(wProductID));
        }
        return wResult;
    }

    public APSMarginDayCapacity getMarginDayCapacity(Calendar wCalendar) {
        String wKey = StringUtils.parseCalendarToString(wCalendar, "yyyy-MM-dd");

        if (DayCapacityMap.containsKey(wKey))
            return DayCapacityMap.get(wCalendar);

        return null;
    }

    public APSMarginEntityCapacity() {
    }

    /**
     * @param wEntityID
     * @param wFMCShiftList
     * @param wCFGCalendarList
     * @param wFPCCapacitySetMap
     * @param wUseCapacity       每个产品 每天 每班多少个
     * @param wStartDate
     * @param wEndDate
     */
    public APSMarginEntityCapacity(int wEntityID, List<FMCShift> wFMCShiftList, List<CFGCalendar> wCFGCalendarList, Map<Integer,
            List<FPCCapacity>> wFPCCapacitySetMap, Map<String, List<APSCapacityUsed>> wUseCapacity, List<FMCUnavailable> wEntityUnavailableList, Calendar wStartDate, Calendar wEndDate) {

        this.EntityID = wEntityID;

        APSMarginDayCapacity wAPSMarginDayCapacity;
        Calendar wStartDateClone = CalendarTool.CloneDate(wStartDate);
        String wDateString = "";
        List<FMCShift> wDayShiftList;
        while (wStartDateClone.compareTo(wEndDate) <= 0) {

            wDateString = StringUtils.parseCalendarToString(wStartDateClone, "yyyy-MM-dd");
            if (DayCapacityMap.containsKey(wDateString))
                continue;
            wDayShiftList = new ArrayList<>();
            for (CFGCalendar wCFGCalendar : wCFGCalendarList) {
                if (CalendarTool.CompareDate(wCFGCalendar.CurrentDay, wStartDateClone) != 0)
                    continue;
                if (wCFGCalendar.IsHoliday == 1)
                    continue;

                if (wCFGCalendar.ShiftList == null)
                    continue;

                for (FMCShift wFMCShift : wFMCShiftList) {
                    if (wFMCShift.HolidayEnable == 1 || wCFGCalendar.ShiftList.contains(wFMCShift.ShiftType))
                        wDayShiftList.add(wFMCShift);
                }
            }

            if (!wUseCapacity.containsKey(wDateString)) {
                wUseCapacity.put(wDateString, new ArrayList<>());
            }
            wAPSMarginDayCapacity = new APSMarginDayCapacity(EntityID, wStartDateClone, wDayShiftList,
                    wFPCCapacitySetMap, wUseCapacity.get(wDateString), wEntityUnavailableList);


            wStartDateClone.add(Calendar.DAY_OF_MONTH,1);
            DayCapacityMap.put(wDateString, wAPSMarginDayCapacity);
        }

    }



    public APSMarginEntityCapacity  Clone(){
        APSMarginEntityCapacity wResult=new APSMarginEntityCapacity();
        wResult.EntityID=this.EntityID;
        wResult.RouteType=this.RouteType;
        for (String wKey:DayCapacityMap.keySet()){
            wResult.DayCapacityMap.put(wKey,DayCapacityMap.get(wKey).Clone());
        }
        return wResult;
    }
}
